﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Eixos3D
{
    /// <summary>
    /// Classe que cria um objeto que desenha os eixos 3d na tela
    /// </summary>
    class classeEixos3D
    {
        /// <summary>
        /// O dispositivo gráfico
        /// </summary>
        private GraphicsDevice device;
 
        /// <summary>
        /// O conjunto de vértices que será desenhado na tela
        /// </summary>
        private VertexBuffer vertexBuffer;

        /// <summary>
        /// O efeito que será utilizado para fazer o desenho 3d
        /// </summary>
        private BasicEffect effect;
    
        /// <summary>
        /// A matriz que representa as transformações do mundo virtual
        /// </summary>
        public Matrix worldMatrix = Matrix.Identity;

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="graphicsDevice">O dispositivo gráfico</param>
        public classeEixos3D(GraphicsDevice graphicsDevice)
        {
            device = graphicsDevice;

            effect = new BasicEffect(device);

            effect.View = Matrix.CreateLookAt
                (new Vector3(0.0f, 0.0f, 3.0f),//posicao
                Vector3.Zero,//apontamento
                Vector3.Up);//orientacao

            effect.Projection = Matrix.CreatePerspectiveFieldOfView
                (MathHelper.ToRadians(45.0f),
                device.Viewport.AspectRatio,
                1.0f,
                10.0f);

            effect.VertexColorEnabled = true;
            
            Create3DAxis();
        }

        /// <summary>
        /// Para limpeza do vertex buffer (array de vértices na memória de vídeo)
        /// </summary>
        public void UnloadContent()
        {
            //if (vertexBuffer != null)
            //{
            //    vertexBuffer.Dispose();
            //    vertexBuffer = null;
            //}

            //if (effect != null)
            //{
            //    effect.Dispose();
            //    effect = null;
            //}
        }

        /// <summary>
        /// Para carregamento e definição do desenho dos eixos 3d
        /// </summary>
        public void LoadContent()
        { 
            
        }

        /// <summary>
        /// Definição do desenho dos eixos 3d
        /// </summary>
        private void Create3DAxis()
        {
            float axisLength = 1f;
            int vertexCount = 6;

            VertexPositionColor[] vertices = new VertexPositionColor[vertexCount];
            
            vertices[0] = new VertexPositionColor(new Vector3(-axisLength, 0.0f, 0.0f), Color.Red);
            vertices[1] = new VertexPositionColor(new Vector3(axisLength, 0.0f, 0.0f), Color.Red);
            
            vertices[2] = new VertexPositionColor(new Vector3(0.0f, -axisLength, 0.0f), Color.Green);
            vertices[3] = new VertexPositionColor(new Vector3(0.0f, axisLength, 0.0f), Color.Green);
            
            vertices[4] = new VertexPositionColor(new Vector3(0.0f, 0.0f, -axisLength), Color.Blue);
            vertices[5] = new VertexPositionColor(new Vector3(0.0f, 0.0f, axisLength), Color.Blue);

            /*
            vertices[6] = new VertexPositionColor(new Vector3(axisLength - 0.1f, 0.05f, 0.0f), Color.White);
            vertices[7] = new VertexPositionColor(new Vector3(axisLength - 0.05f, 0.2f, 0.0f), Color.White);
            vertices[8] = new VertexPositionColor(new Vector3(axisLength - 0.05f, 0.05f, 0.0f), Color.White);
            vertices[9] = new VertexPositionColor(new Vector3(axisLength - 0.1f, 0.2f, 0.0f), Color.White);

            vertices[10] = new VertexPositionColor(new Vector3(0.075f, axisLength - 0.125f, 0.0f), Color.White);
            vertices[11] = new VertexPositionColor(new Vector3(0.075f, axisLength - 0.2f, 0.0f), Color.White);
            vertices[12] = new VertexPositionColor(new Vector3(0.075f, axisLength - 0.125f, 0.0f), Color.White);
            vertices[13] = new VertexPositionColor(new Vector3(0.1f, axisLength - 0.05f, 0.0f), Color.White);
            vertices[14] = new VertexPositionColor(new Vector3(0.075f, axisLength - 0.125f, 0.0f), Color.White);
            vertices[15] = new VertexPositionColor(new Vector3(0.05f, axisLength - 0.05f, 0.0f), Color.White);

            vertices[16] = new VertexPositionColor(new Vector3(0.0f, 0.05f, axisLength - 0.1f), Color.White);
            vertices[17] = new VertexPositionColor(new Vector3(0.0f, 0.05f, axisLength - 0.05f), Color.White);
            vertices[18] = new VertexPositionColor(new Vector3(0.0f, 0.05f, axisLength - 0.1f), Color.White);
            vertices[19] = new VertexPositionColor(new Vector3(0.0f, 0.2f, axisLength - 0.05f), Color.White);
            vertices[20] = new VertexPositionColor(new Vector3(0.0f, 0.2f, axisLength - 0.1f), Color.White);
            vertices[21] = new VertexPositionColor(new Vector3(0.0f, 0.2f, axisLength - 0.05f), Color.White);
            */

            vertexBuffer = new VertexBuffer(device, VertexPositionColor.VertexDeclaration, vertexCount, BufferUsage.WriteOnly);

            vertexBuffer.SetData<VertexPositionColor>(vertices);
        }

        /// <summary>
        /// Desenho dos eixos 3d
        /// </summary>
        public void Draw()
        {
            effect.World = worldMatrix;

            device.SetVertexBuffer(vertexBuffer);
    
            foreach(EffectPass CurrentPass in effect.CurrentTechnique.Passes)
            {
                CurrentPass.Apply();
                device.DrawPrimitives(PrimitiveType.LineList, 0, 3);
            }
        }
    }
}